package com.changgou.file.controller;

import com.changgou.entity.Result;
import com.changgou.file.util.FastDFSClient;
import com.changgou.file.util.FastDFSFile;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/file")
public class FileController {
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        byte[] fileBytes = file.getBytes();
        String ext = filename.substring(filename.lastIndexOf(".")+1);
        //构建一个fastDFSFile对象
        FastDFSFile fastDFSFile = new FastDFSFile(filename,fileBytes,ext);
        //使用工具类上传文件到fastDFS
        String[] upload = FastDFSClient.upload(fastDFSFile);

        String url = FastDFSClient.getTrackerUrl()+upload[0]+"/"+upload[1];
        return Result.success("上传成功",url);
    }
}
